package com.itheima.redbaby.activity;

import java.util.List;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.R;
import com.itheima.redbaby.BaseActivity.MyHttpTask;
import com.itheima.redbaby.activity.LimitBuyActivity.ViewHolder;
import com.itheima.redbaby.bean.Product;
import com.itheima.redbaby.bean.Topic;
import com.itheima.redbaby.engine.TopicServletPListEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;
import com.itheima.redbaby.utils.ImageUtil;
import com.itheima.redbaby.utils.PromptManager;

/**
 * 促销快报 商品列表
 * 
 * @author xun
 * 
 */
public class CuXiaoProductListActivity extends BaseActivity {

	private GridView my_produit_list;
	private  TextView  backTv;
	private MyProductGridAdapter gridadpter;// 第二级 gridview 其中数据时 商品列表中华的数据

	private String TopicID; // 从促销快报=传送过来的 活动主题id

	private List<Product> gridData;

	private TopicServletPListEngine impl;

	/* ==========在商品列表中需要的数据============= */
	private int productId; // 产品id
	private String productName; // 产品名字
	private float marketprice; // 产品市场价格
	private float price; // 产品会员价
	private String pic; // 产品图片地址
	/* ======================= */

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cuxiao_product);
		Intent intent = getIntent();
		TopicID = intent.getStringExtra("topicId");
		my_produit_list = (GridView) findViewById(R.id.prdoct_grid);
		gridadpter = new MyProductGridAdapter();
		// 获取第一级 listview 数据
		impl = DaoAndEngineFactory.getImpl(TopicServletPListEngine.class);
		fillListData();
		backTv = (TextView) findViewById(R.id.backTv);

		backTv.setOnClickListener( new  OnClickListener() {
				
				@Override
				public void onClick(View v) {
					finish();
				}
			});

	}

	public void fillListData() {
		new MyHttpTask() {

			protected void onPreExecute() {
				PromptManager.showProgressDialog(CuXiaoProductListActivity.this);
			};
			/***
			 * 获取后台数据
			 */
			@Override
			protected Object doInBackground(Void... params) {

				List<Product> topicPList = impl.getTopicPList(TopicID, 1, 15);
				System.out.println(topicPList.toString());
				return topicPList;

			}

			/**
			 * 获取后台返回 的数据 并且跟新ui 获得的是 topic 对象
			 * 
			 * 更新 holder.textContent.setText("酷 ~宝贝  ~宝贝~ 裤" + "时间");
			 * holder.imgIcon.setImageResource(imgsid[position]);
			 */
			@Override
			protected void onPostExecute(Object result) {
				gridData = (List<Product>) result;
				System.out.println(result.toString());
				for (int i = 0; i < gridData.size(); i++) {
					Product product = gridData.get(i);
					productId = product.getId();
					productName = product.getName();
					marketprice = product.getMarketprice();
					price = product.getPrice();
					pic = product.getPic();

				}
				my_produit_list.setAdapter(gridadpter);
				gridadpter.notifyDataSetChanged();
				PromptManager.closeProgressDialog();
			}

		}.executeHttp();

		/**
		 * listview 中的跳转事件 点击时候我们 跳转到商品列表页面
		 */
		my_produit_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				 Product product = gridData.get(position);
				 int productId = product.getId();
				 Intent intent = new Intent(CuXiaoProductListActivity.this,
						 ProductDetailActivity.class);
				 intent.putExtra("pId", productId);
				 startActivity(intent);
			}
		});
	}

	

	class MyProductGridAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return gridData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v;

			ViewHolder holder;
			if (convertView != null) {
				v = convertView;
				holder = (ViewHolder) v.getTag();
			} else {
				v = View.inflate(CuXiaoProductListActivity.this,
						R.layout.grid_data, null);
				holder = new ViewHolder();
				holder.prod_img_data = (ImageView) v
						.findViewById(R.id.prod_img_data);

				holder.prod_price_data = (TextView) v
						.findViewById(R.id.prod_price_data);
				holder.prod_price = (TextView) v.findViewById(R.id.prod_price);
				holder.prod_name_data = (TextView) v
						.findViewById(R.id.prod_name_data);

				v.setTag(holder);
			}
			ImageUtil.loadImageFromUrl(holder.prod_img_data, pic);
			//holder.prod_img_data.setImageResource(imageid[position]);
			holder.prod_name_data.setText(productName);
			holder.prod_price.setText(price + "");
			return v;
		}

	}

	static class ViewHolder {
		TextView prod_price_data;
		TextView prod_price;
		TextView prod_name_data;
		ImageView prod_img_data;

	}

}
